package com.zwface.demo.function.leave.mvvm;

import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.zwface.demo.App;
import com.zwface.demo.base.BaseViewModel;
import com.zwface.demo.comm.comm;
import com.zwface.demo.function.leave.LeaveActivity;
import com.zwface.demo.greendao.LeaveManager;
import com.zwface.demo.model.db.Leave;
import com.zwface.demo.model.db.UserInfo;
import com.zwface.demo.utils.FastjsonHelper;
import com.zwface.demo.utils.SharedPreferenceUtils;

/**
 * created by      张中文
 * on:             2020/4/29 10:56
 * 描述：
 */
public class LeaveViewModel extends BaseViewModel
{
    public ObservableField<String> reason = new ObservableField();
    public ObservableField<String> timeStart = new ObservableField();
    public ObservableField<String> timeEnd = new ObservableField();

    /**
     * 提交请假申请
     */
    public void submitLeave(){
        String rea = reason.get();
        String time_s = timeStart.get();
        String time_e = timeEnd.get();
        if(!comm.ValidationForm(rea,"请输入请假理由")) return;
        if(!comm.ValidationForm(time_s,"请输入开始时间")) return;
        if(!comm.ValidationForm(time_e,"请输入结束时间")) return;
        Leave leave = new Leave();
        leave.setReason(rea);
        leave.setTimeStart(TimeUtils.string2Millis("2020-"+time_s+":00"));
        leave.setTimeEnd(TimeUtils.string2Millis("2020-"+time_e+":00"));
        String userId = SharedPreferenceUtils.getParam(App.getInstance().getApplicationContext(),SharedPreferenceUtils.USER_ID);
        String userName = SharedPreferenceUtils.getParam(App.getInstance().getApplicationContext(),SharedPreferenceUtils.USER_NAME);
        leave.setStuId(Long.parseLong(userId));
        leave.setStuName(userName);

        LeaveManager.addLeave(leave);
        ToastUtils.showShort("提交成功，等待审核");
        ActivityUtils.finishActivity(LeaveActivity.class);
    }
}
